package com.klangner.go.model.impl;

import com.klangner.go.model.GameStatus;
import com.klangner.go.model.IBoard;
import com.klangner.go.model.IGameState;
import com.klangner.go.model.IRuleChecker;
import com.klangner.go.model.Move;

public class GamesState implements IGameState {

	/** Game board */
	private IBoard				board;
	/** Game status */
	private GameStatus		status;
	/** Game rules */
	private IRuleChecker	rules;
	/** Last move info */
	private Move					lastMove;
	
	
	/**
	 * Constructor
	 */
	public GamesState(int boardSize, IRuleChecker rules){
		
		this.rules = rules;
		board = new GoBoard(boardSize);
		status = GameStatus.IN_PROGRESS;
	}
	
	@Override
	public IBoard getBoard() {
		return board;
	}

	@Override
	public boolean isLegalMove(Move move) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public GameStatus getStatus() {
		return status;
	}

	@Override
	public int getTurnColor() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public IGameState makeCopy() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * Make only legal moves.
	 * Invalid move == lost game
	 * This function also removes dead stones
	 */
	@Override
	public void makeMove(Move move) {

		if( !rules.isLegalMove(board, move) ){
			if(move.getColor() == IBoard.BLACK_STONE)
				status = GameStatus.WHITE_WON;
			else
				status = GameStatus.BLACK_WON;
		}
		else if(lastMove == null && move.getColor() == IBoard.WHITE_STONE){
			// Black first
			status = GameStatus.BLACK_WON;
		}
		else if(lastMove != null && lastMove.getColor() == move.getColor()){
			if(move.getColor() == IBoard.BLACK_STONE)
				status = GameStatus.WHITE_WON;
			else
				status = GameStatus.BLACK_WON;
		}
		else{
			if(!move.isPass()){
				lastMove = move;
				board.setStone(move.getX(), move.getY(), move.getColor());
			}
		}
	}

}
